package warmachineroster.presentation.wizards;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;

import warmachinesystem.ArmyList;
import warmachinesystem.GameSystem;
import warmachinesystem.WarmachineSystemPlugin;
import warmachinesystem.presentation.WarmachineSystemEditorPlugin;
import warmachinesystem.provider.ArmyListContentProvider;
import warmachinesystem.provider.ArmyListTableLabelProvider;

public class WarmachineRosterCreationPage extends WizardPage {
	private TableViewer armyListViewer;
	private Text rosterName;
	private Text rosterPoints;
	
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected ModifyListener validator =
		new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				setPageComplete(validatePage());
			}
		};
	
	/**
	 * Create the wizard.
	 */
	public WarmachineRosterCreationPage() {
		super("wizardPage");
	}

	protected boolean validatePage() {
		String name = getRosterName();
		if (name.length() == 0)
			return false;
		IFile rosterFile = getRosterFile();
		if (rosterFile.exists()){
			return false;
		}
		
		String pointValueStr = getRosterPoints();
		try{
			int pointValue = Integer.parseInt(pointValueStr);
			if (pointValue <= 0)
				return false;
		}catch (NumberFormatException e){
			return false;
		}
		
		if(armyListViewer.getSelection().isEmpty()){
			return false;
		}
		
		return true;
	}

	protected IFile getRosterFile() {
		IProject wmProject =  WarmachineSystemEditorPlugin.INSTANCE.getRootProject();
		IFile rosterFile = wmProject.getFile(getRosterName() + ".wmr");
		return rosterFile;
	}

	protected String getRosterPoints() {
		return rosterPoints.getText();
	}

	protected String getRosterName() {
		return rosterName.getText();
	}

	/**
	 * Create contents of the wizard.
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);
		container.setLayout(new GridLayout(1, false));
		
		Composite composite = new Composite(container, SWT.NONE);
		GridLayout gl_composite = new GridLayout(2, true);
		gl_composite.verticalSpacing = 12;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite.setBounds(0, 0, 64, 64);
		
		createArmyListViewer(composite);
		createRightPanel(composite);
		setPageComplete(false);

	}

	private void createRightPanel(Composite composite) {
		Composite rightComposite = new Composite(composite, SWT.NONE);
		FillLayout fl_rightComposite = new FillLayout(SWT.VERTICAL);
		fl_rightComposite.marginHeight = 10;
		fl_rightComposite.spacing = 15;
		rightComposite.setLayout(fl_rightComposite);
		rightComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1));
		
		Composite nameComposite = new Composite(rightComposite, SWT.NONE);
		FillLayout fl_nameComposite = new FillLayout(SWT.HORIZONTAL);
		fl_nameComposite.marginWidth = 20;
		fl_nameComposite.spacing = 10;
		nameComposite.setLayout(fl_nameComposite);
		
		Label nameLabel = new Label(nameComposite, SWT.NONE);
		nameLabel.setText("Name");
		
		rosterName = new Text(nameComposite, SWT.BORDER);
		
		rosterName.addModifyListener(validator);
		
		Composite pointsComposite = new Composite(rightComposite, SWT.NONE);
		FillLayout fl_pointsComposite = new FillLayout(SWT.HORIZONTAL);
		fl_pointsComposite.marginWidth = 20;
		fl_pointsComposite.spacing = 10;
		pointsComposite.setLayout(fl_pointsComposite);
		
		Label pointsLabel = new Label(pointsComposite, SWT.NONE);
		pointsLabel.setText("Point Value");
		
		rosterPoints = new Text(pointsComposite, SWT.BORDER);
		rosterPoints.addModifyListener(validator);
	}

	private void createArmyListViewer(Composite composite) {
		armyListViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
		Table armyListTable = armyListViewer.getTable();
		{
			GridData gd_armyListTable = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
			gd_armyListTable.widthHint = 261;
			armyListTable.setLayoutData(gd_armyListTable);
		}
		armyListViewer.setLabelProvider(new ArmyListTableLabelProvider());
		armyListViewer.setContentProvider(new ArmyListContentProvider());
		armyListViewer.setInput(getInitialInput());
		armyListViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				setPageComplete(validatePage());			
			}
		});
	}
	
	
	private GameSystem getInitialInput() {
		GameSystem gs = WarmachineSystemPlugin.INSTANCE.getGameSystem();
		return gs;
	}

	public ArmyList getArmyList(){
		ArmyList result = null;
		IStructuredSelection selectedArmyList = (IStructuredSelection) armyListViewer.getSelection();
		result = (ArmyList) selectedArmyList.getFirstElement();
		
		return result;
	}
	
	public int getPointValue(){
		return Integer.parseInt(getRosterPoints());
	}
	
	
}
